
from rest_framework.pagination import PageNumberPagination


class DynamicPageNumberPagination(PageNumberPagination):
    """
    动态分页类：通过size参数控制是否分页
    ?size=all 获取全部数据
    ?size=12 正常分页
    """
    page_size_query_param = 'size'

    def paginate_queryset(self, queryset, request, view=None):
        if request.query_params.get(self.page_size_query_param, '').lower() == 'all':
            return None  # 返回None表示禁用分页
        return super().paginate_queryset(queryset, request, view)